package springapp.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import springapp.entity.CompanyEntity;
import springapp.service.CompanyService;
import springapp.util.Utils;

@Controller
public class GetAllCompaniesController implements IControllerConstants {

	@Autowired(required = true)
	private CompanyService companyService;

	@RequestMapping(value = "/getAllCompanies.htm", method = RequestMethod.GET)
	public @ResponseBody
	String handleRequest() {

		List<CompanyEntity> companies = companyService.getAllCompanies();
		return Utils.getJson(getData(companies));
	}

	private List<Company> getData(List<CompanyEntity> data) {
		List<Company> companies = new ArrayList<Company>();
		for (CompanyEntity companyEntity : data) {
			companies.add(new Company(companyEntity));
		}
		return companies;
	}
}

class Company {
	private Integer companyId;
	private String companyName;
	private String headquarters;
	private Float rating = (float) 0;
	private String industryName;
	private Set<String> locations;


	public Company(CompanyEntity entity) {
		this.companyId = entity.getCompanyId();
		this.companyName = entity.getCompanyName();
		this.headquarters = entity.getHeadquarters();
		this.rating = entity.getRating();
		this.industryName = entity.getIndustryName();
		this.locations = Utils.getNamesFromCacheEntity(entity.getLocations());
	}

	public Integer getCompanyId() {
		return companyId;
	}

	public void setCompanyId(Integer companyId) {
		this.companyId = companyId;
	}

	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	public String getHeadquarters() {
		return headquarters;
	}

	public void setHeadquarters(String headquarters) {
		this.headquarters = headquarters;
	}

	public Float getRating() {
		return rating;
	}

	public void setRating(Float rating) {
		this.rating = rating;
	}

	public String getIndustryName() {
		return industryName;
	}

	public void setIndustryName(String industryName) {
		this.industryName = industryName;
	}
	
	public Set<String> getLocations() {
		return locations;
	}
	
	public void setLocations(Set<String> locations) {
		this.locations = locations;
	}
}
